package com.apimanage.auth.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import java.util.ArrayList;
import java.util.List;



/**
 * Spring Security配置类
 * 替代Shiro实现JWT认证功能
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig {

    // 不需要注入JwtTokenUtil，使用静态方法
    
    // 使用ConfigurationProperties加载白名单配置
    @ConfigurationProperties(prefix = "gateway.auth")
    @Component
    public static class AuthProperties {
        private List<String> whitelist = new ArrayList<>();
        
        public List<String> getWhitelist() {
            return whitelist;
        }
        
        public void setWhitelist(List<String> whitelist) {
            this.whitelist = whitelist;
        }
    }
    
    @Autowired
    private AuthProperties authProperties;


    /**
     * 密码编码器
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 认证管理器
     */
    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }

    /**
     * 配置Spring Security过滤器链
     */
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        // 配置URL权限
        http
            // 禁用CSRF保护，因为我们使用JWT
            .csrf(csrf -> csrf.disable())
            // 不创建会话，使用JWT进行无状态认证
            .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
            // 配置URL权限
            .authorizeHttpRequests(authorize -> authorize
                // 允许匿名访问的路径 - 从配置文件读取白名单
                .requestMatchers(authProperties.getWhitelist().toArray(new String[0])).permitAll()
                // 其他所有路径都需要认证
                .anyRequest().authenticated()
            )
            // 添加JWT认证过滤器
            .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);

        return http.build();
    }

    /**
     * JWT认证过滤器
     */
    // 直接使用容器中的JwtAuthenticationFilter实例
    @Autowired
    private JwtAuthenticationFilter jwtAuthenticationFilter;
}